package cn.didi.feidi.javabackend.web.waterdeliverapp.controller.waterdeliver;

import cn.didi.feidi.javabackend.mysql.jpa.exception.MyException;
import cn.didi.feidi.javabackend.mysql.jpa.hibernate.ConditionWrapper;
import cn.didi.feidi.javabackend.mysql.jpa.response.ApiResponse;
import cn.didi.feidi.javabackend.server.waterdeliver.annotation.NeedVaild;
import cn.didi.feidi.javabackend.server.waterdeliver.annotation.TokenVaild;
import cn.didi.feidi.javabackend.server.waterdeliver.constant.message.exception.LoginConstants;
import cn.didi.feidi.javabackend.server.waterdeliver.constant.message.exception.RegisterConstants;
import cn.didi.feidi.javabackend.server.waterdeliver.dto.LoginInfo;
import cn.didi.feidi.javabackend.server.waterdeliver.dto.RegisterInfo;
import cn.didi.feidi.javabackend.server.waterdeliver.dto.UpdateInfo;
import cn.didi.feidi.javabackend.server.waterdeliver.service.WaterDeliverService;
import org.apache.commons.lang.IllegalClassException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.Table;
import java.util.*;

/**
 * Created by Administrator on 2017/7/16.
 */
@RestController
@RequestMapping("/waterDeliver")
public class WaterDeliverController {
    @Autowired
    WaterDeliverService waterDeliverService;

    /**
     * 处理注册请求
     * @param registerInfo
     * @return
     * @throws MyException
     */
    @RequestMapping(value="register",method = RequestMethod.POST)
    public ApiResponse<String> register( RegisterInfo registerInfo) throws MyException ,Exception{
        validateRegister(registerInfo);
        waterDeliverService.register(registerInfo);
        ApiResponse<String> response = new ApiResponse<String>("0", "success");
        return null;
    }

    /**
     * 处理登录请求
     * @param loginInfo
     * @return
     */
   @RequestMapping(value="login",method = RequestMethod.POST)
   public ApiResponse<LoginInfo> login(LoginInfo loginInfo) throws MyException {
       validateLogin(loginInfo);
       LoginInfo result= waterDeliverService.login(loginInfo);
       ApiResponse<LoginInfo> response = new ApiResponse<>("0", "success");
//       response.setObjList(result);
       response.setObj(result);
       return response;
   }

    /**
     * 更新信息
     * @param updateInfo
     * @return
     * @throws MyException
     */
    @TokenVaild
    @RequestMapping(value="update",method = RequestMethod.POST)
    public ApiResponse<String> update(@NeedVaild UpdateInfo updateInfo) throws MyException {
        //
        System.out.println("catch update......");
        ApiResponse<String> response = new ApiResponse<>("0", "success");
        return response;
    }
    /**
     *验证注册信息
     * @param registerInfo
     * @throws MyException
     */
    private void validateRegister(RegisterInfo registerInfo) throws MyException {
        String errorMessage = null;
        if(StringUtils.isEmpty(registerInfo.getPhoneNumber()))
            errorMessage=RegisterConstants.NONE_PHONE_NUMBER;
        else if (StringUtils.isEmpty(registerInfo.getPassword()))
            errorMessage=RegisterConstants.NONE_PASSWORD;
        else  if(registerInfo.getVerificationCode()==null||!registerInfo.getVerificationCode().equals("123456"))
            errorMessage=RegisterConstants.NULL_VERIFICATION_CODE;
        if(!StringUtils.isEmpty(errorMessage))
            throw new MyException(errorMessage.toString());
    }

    /**
     * 验证登录信息
     * @param loginInfo
     * @throws MyException
     */
    private void validateLogin(LoginInfo loginInfo) throws MyException {
       String errorMessage=null;
       if(loginInfo.getPhoneNumber()==null|| loginInfo.getPhoneNumber().equals(""))
           errorMessage= LoginConstants.NONE_PHONE_NUMBER;
       else if (loginInfo.getPassword()==null|| loginInfo.getPassword().equals(""))
           errorMessage= LoginConstants.NONE_PASSWORD;
        if(!StringUtils.isEmpty(errorMessage))
            throw new MyException(errorMessage.toString());

    }

    /**
     * 多表查询
     */
//    public void test(List<ConditionWrapper> conditions) throws Exception{
//        String modelPackage = "cn.didi.feidi.javabackend.mysql.jpa";
//        Map<String, ConditionWrapper> conditionsMap = new LinkedHashMap<>();
//        for(ConditionWrapper condition : conditions){
//            String className = condition.getClassName();
//            if(!conditionsMap.containsKey(className)){
//                Class<?> clazz = Class.forName(modelPackage + "." + className);
//                condition.setTableName(clazz.getAnnotation(Table.class).name());
//                conditionsMap.put(className, condition);
//            }
//            else{
//                ConditionWrapper oldCondition = conditionsMap.get(className);
//                oldCondition.addOperations(condition.getOperations());
//            }
//        }
//        waterDeliverService.test(conditionsMap);
//    }

    public void test(ConditionWrapper... conditions) throws Exception {
        List<ConditionWrapper> conditionsList = Arrays.asList(conditions);
        Collections.sort(conditionsList, ConditionWrapper.getComparator());
        waterDeliverService.parse(conditionsList);
    }
}

